package com.wpzf.zxhc.utils;

import com.wpzf.zxhc.domain.WpCjTbxxbxj2023DTO;
import com.wpzf.zxhc.domain.YtbTbInfo;
import org.gdal.gdal.gdal;
import org.gdal.ogr.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.gdal.ogr.ogr;
import org.gdal.osr.SpatialReference;


public class GdalUtils
{
    public String exportTbList(HttpServletResponse response, List<WpCjTbxxbxj2023DTO> dataList,String tempath) {
        ogr.RegisterAll();
        String rest="";
        try {
        String nowDate = new SimpleDateFormat("yyyyMMddHHMMss").format(new Date());
        String shapefilePath=tempath + "/图斑列表" + nowDate + ".shp";
//        gdal.SetConfigOption("GDAL_DATA","D:\\project\\周报\\GDAL_Release");

        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
        gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8");
        gdal.SetConfigOption("SHAPE_ENCODING","CP936");

        Driver driver = ogr.GetDriverByName("ESRI Shapefile");
        if (driver == null) {
            System.out.println("打开驱动器失败");
            return "error,打开驱动器失败";
        }


            /*获取核查结果*/
            DataSource dataSource = driver.CreateDataSource(shapefilePath);
            if (dataSource == null) {
                System.out.println("创建数据源失败");
                return "error,创建数据源失败";
            }
            // 定义坐标系
            SpatialReference spatialReference = new SpatialReference("");
            spatialReference.SetWellKnownGeogCS("WGS84");
//            SpatialReference.ImportFromEPSG(4490);

             // 创建一个新的图层
            Layer layer = dataSource.CreateLayer("new_layer",spatialReference,ogr.wkbPolygon,null);

            // 添加新字段 smc
            FieldDefn smc = new FieldDefn("smc", ogr.OFTString);
            smc.SetWidth(254);
            layer.CreateField(smc);

            FieldDefn smc1 = new FieldDefn("smc1", ogr.OFTString);
            smc1.SetWidth(254);
            layer.CreateField(smc1);

            FieldDefn qxmc = new FieldDefn("qxmc", ogr.OFTString);
            qxmc.SetWidth(254);
            layer.CreateField(qxmc);

            FieldDefn ssxz = new FieldDefn("ssxz", ogr.OFTString);
            qxmc.SetWidth(254);
            layer.CreateField(ssxz);

            FieldDefn ssc = new FieldDefn("ssc", ogr.OFTString);
            qxmc.SetWidth(254);
            layer.CreateField(ssc);

            FieldDefn jcbh = new FieldDefn("jcbh", ogr.OFTString);
            qxmc.SetWidth(254);
            layer.CreateField(jcbh);

            FieldDefn iscl = new FieldDefn("iscl", ogr.OFTString);
            qxmc.SetWidth(254);
            layer.CreateField(iscl);

            FieldDefn jcmj = new FieldDefn("jcmj", ogr.OFTReal);
            layer.CreateField(jcmj);

            FieldDefn jctbxzgdmj = new FieldDefn("jctbxzgdmj", ogr.OFTReal);
            layer.CreateField(jctbxzgdmj);

            FieldDefn jctbxznydzmj = new FieldDefn("xznydzmj", ogr.OFTReal);
            layer.CreateField(jctbxznydzmj);

            FieldDefn zjbntmj = new FieldDefn("zjbntmj", ogr.OFTReal);
            layer.CreateField(zjbntmj);


            for (WpCjTbxxbxj2023DTO item : dataList)
            {
                 //创建一个新的几何对象，这里是点
                Feature feature = new Feature(layer.GetLayerDefn());
                if(item.getWkt()!="")
                {
//                    System.out.println(item.getWkt().split(";")[0].toString());
                    feature.SetGeometry(ogr.CreateGeometryFromWkt(item.getWkt().split(";")[0].toString()));
                }
                feature.SetField("smc", item.getSmc());
                feature.SetField("smc1", item.getSmc1());
                feature.SetField("qxmc", item.getQxmc());
                feature.SetField("ssxz", item.getMjzbzddxzjxzqmc());
                feature.SetField("ssc", item.getMjzbzddxzjxzqmc());
                feature.SetField("jcbh", item.getJcbh());

               String iscl1="";
                if ("1".equals(item.getIscl())) {
                    iscl1 = "已填报";
                } else if ("2".equals(item.getIscl())) {
                    iscl1 = "未填报";
                }
                feature.SetField("iscl", iscl1);
                feature.SetField("jcmj", item.getJcmj());
                feature.SetField("jctbxzgdmj", item.getJctbxzgdmj());
                feature.SetField("xznydzmj", item.getJctbxznydzmj());
                feature.SetField("zjbntmj", item.getZjbntmj());

                //添加特征到图层
                layer.CreateFeature(feature);
            }
            layer.SyncToDisk();
            rest = "图斑列表" + nowDate + ".shp";
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            rest = "error";
        }
        return rest;
    }


    public String exportDkList(HttpServletResponse response,  List<YtbTbInfo> dataList,String tempath) {
        ogr.RegisterAll();
        String rest="";
        try {
            String nowDate = new SimpleDateFormat("yyyyMMddHHMMss").format(new Date());
            String shapefilePath=tempath + "/地块列表" + nowDate + ".shp";

            gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
            gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8");
            gdal.SetConfigOption("SHAPE_ENCODING","CP936");

            Driver driver = ogr.GetDriverByName("ESRI Shapefile");
            if (driver == null) {
                System.out.println("打开驱动器失败");
                return "error,打开驱动器失败";
            }


            //有要素
                /*获取核查结果*/
                DataSource dataSource = driver.CreateDataSource(shapefilePath);
                if (dataSource == null) {
                    System.out.println("创建数据源失败");
                    return "error,创建数据源失败";
                }
                // 定义坐标系
                SpatialReference spatialReference = new SpatialReference("");
                spatialReference.SetWellKnownGeogCS("WGS84");

                // 创建一个新的图层
                Layer layer = dataSource.CreateLayer("new_layer",spatialReference,ogr.wkbPolygon,null);

                // 添加新字段 smc
                FieldDefn smc = new FieldDefn("smc", ogr.OFTString);
                smc.SetWidth(254);
                layer.CreateField(smc);

                FieldDefn smc1 = new FieldDefn("smc1", ogr.OFTString);
                smc1.SetWidth(254);
                layer.CreateField(smc1);

                FieldDefn qxmc = new FieldDefn("qxmc", ogr.OFTString);
                qxmc.SetWidth(254);
                layer.CreateField(qxmc);

                FieldDefn ssxz = new FieldDefn("ssxz", ogr.OFTString);
                ssxz.SetWidth(254);
                layer.CreateField(ssxz);


                FieldDefn ssc = new FieldDefn("ssc", ogr.OFTString);
                ssc.SetWidth(254);
                layer.CreateField(ssc);


                FieldDefn dkbh = new FieldDefn("dkbh", ogr.OFTString);
                dkbh.SetWidth(254);
                layer.CreateField(dkbh);


                FieldDefn jcbh = new FieldDefn("jcbh", ogr.OFTString);
                jcbh.SetWidth(254);
                layer.CreateField(jcbh);

                FieldDefn hcjd = new FieldDefn("hcjd", ogr.OFTString);
                hcjd.SetWidth(254);
                layer.CreateField(hcjd);

                FieldDefn pdlx = new FieldDefn("pdlx", ogr.OFTString);
                pdlx.SetWidth(254);
                layer.CreateField(pdlx);

                FieldDefn jtqx = new FieldDefn("jtqx", ogr.OFTString);
                jtqx.SetWidth(254);
                layer.CreateField(jtqx);

                FieldDefn sjyt = new FieldDefn("sjyt", ogr.OFTString);
                sjyt.SetWidth(254);
                layer.CreateField(sjyt);

                FieldDefn pdyjsm = new FieldDefn("pdyjsm", ogr.OFTString);
                pdyjsm.SetWidth(254);
                layer.CreateField(pdyjsm);


                FieldDefn thdkmj = new FieldDefn("thdkmj", ogr.OFTReal);
                layer.CreateField(thdkmj);

                FieldDefn thdkgdmj = new FieldDefn("thdkgdmj", ogr.OFTReal);
                layer.CreateField(thdkgdmj);


                FieldDefn jbntmj = new FieldDefn("jbntmj", ogr.OFTReal);
                layer.CreateField(jbntmj);

                FieldDefn thnydmj = new FieldDefn("thnydmj", ogr.OFTReal);
                layer.CreateField(thnydmj);


                for (YtbTbInfo item : dataList)
                {
                    //创建一个新的几何对象，这里是点
                    Feature feature = new Feature(layer.GetLayerDefn());
                    if(item.getWkt()!="")
                    {
//                    System.out.println(item.getWkt().split(";")[0].toString());
                        feature.SetGeometry(ogr.CreateGeometryFromWkt(item.getWkt().split(";")[0].toString()));
                    }
                    feature.SetField("smc", item.getSheng());
                    feature.SetField("smc1",item.getShi());
                    feature.SetField("qxmc",item.getXian());
                    feature.SetField("ssxz",item.getXz());
                    feature.SetField("ssc",item.getCun());
                    feature.SetField("dkbh",item.getDkbh());
                    feature.SetField("jcbh",item.getJcbh());
                    String hcjd1 = "";
                    if ("1".equals(item.getDkzt())) {
                        hcjd1 = "已填报";
                    } else if ("0".equals(item.getDkzt())) {
                        hcjd1 = "未填报";
                    } else if ("2".equals(item.getDkzt())) {
                        hcjd1 = "市级审核";
                    } else if ("3".equals(item.getDkzt())) {
                        hcjd1 = "省级审核";
                    } else if ("4".equals(item.getDkzt())) {
                        hcjd1 = "市级退回县级";
                    } else if ("5".equals(item.getDkzt())) {
                        hcjd1 = "市级复审";
                    } else if ("6".equals(item.getDkzt())) {
                        hcjd1 = "省级复审";
                    } else if ("7".equals(item.getDkzt())) {
                        hcjd1 = "省级退回县级";
                    } else if ("8".equals(item.getDkzt())) {
                        hcjd1 = "完结";
                    }
                    feature.SetField("hcjd",hcjd1);
                    feature.SetField("pdlx",item.getPdlx());
                    feature.SetField("jtqx",item.getJtqx());
                    feature.SetField("sjyt",item.getSjyt());
                    feature.SetField("pdyjsm",item.getPdyjsm());
                    feature.SetField("thdkmj",item.getThdkmj());
                    feature.SetField("thdkgdmj",item.getThdkgdmj());
                    feature.SetField("jbntmj",item.getThdkzjbntmj());
                    feature.SetField("thnydmj",item.getThnydmj());
                    //添加特征到图层
                    layer.CreateFeature(feature);
                }
                layer.SyncToDisk();
            rest = "地块列表" + nowDate + ".shp";
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            rest = "error";
        }
        return rest;
    }

}
